<?php

class Mcart extends CI_Model {
	
	public $table = 'order';
	public $table_b="order_detail";
	public $pri_key="order_id";
	public $pri_key_2="customer_id";
	public $pri_key_b_="prod_id";



	function __construct()

	{

		parent::__construct();

	}
	function get_last_order()
	{
		$this->db->select($this->pri_key)->from($this->table)->order_by($this->pri_key," DESC")->limit(1);
		$query = $this->db->get();
		$data = $query->row_array();
		//print_r($data);
		$id= $data[$this->pri_key];
		return $id;
	}
	public function insert($data,$table=1){
		if($table == 2)
		{
			if($this->db->insert($this->table_b,$data))
			return true;
		
		}
		else
		{
			if($this->db->insert($this->table,$data))
			return true;
		
		}
	
		
		return false;

	}
		function update($data,$id)
	{

		if(is_numeric($id) && $id !='')
		{
			$this->db->where($this->pri_key,$id);
			if($this->db->update($this->table,$data))
				return true;
			return false;
			
		}
		return false;
	}
	
	
	public function info_from_id($id,$table=1)
	{
		if($table == 2)
		{
		if(is_numeric($id) && $id !="")
		{
		$this->db->where($this->pri_key.' =',$id);
		$this->db->join("products","products.id = order_detail.prod_id");
		return $this->db->get($this->table_b)->result_array();
		}
		else
			return false;
		}
		else
		{
				if(is_numeric($id) && $id !="")
			{
			
				
					$this->db->where($this->pri_key.' =',$id);
				//	$this->db->join("customers","customers.id = order.customer_id");
					return $this->db->get($this->table)->row_array();
				
			}
			else
				return false;
		}
	}
	public function list_data_deliver($area,$num,$off)
	{
		$this->db->limit($off,$num);
			if($area > 0 && is_numeric($area) )
			$this->db->where("area = $area and delivered = 1 and deleted = 0  ");
		else
			$this->db->where("delivered = 1 and deleted = 0 ");
		
//			$this->db->where("delivered = 1 and deleted = 0 ");
		//	$this->db->join("customers","customers.id = order.customer_id");
					$this->db->order_by($this->pri_key,"DESC");
				//	print_r($this->db->where());
				$query= $this->db->get($this->table);
				if($query->num_rows() >0)				
				{
			
		
					return $query->result_array();
				}
				return false;
	}
	public function list_data_deleted($area,$num,$off)
	{
		$this->db->limit($off,$num);

		if($area > 0 && is_numeric($area) )
			$this->db->where("area = $area and deleted = 1  ");
		else
			$this->db->where("deleted = 1  ");
		//	$this->db->join("customers","customers.id = order.customer_id");
				$this->db->order_by($this->pri_key,"DESC");
				$query= $this->db->get($this->table);
				if($query->num_rows() >0)				
				{
			
		
					return $query->result_array();
				}
				return false;
	}
	public function list_data_undeliver($area,$num,$off)
	{
		$this->db->limit($off,$num);
		if($area > 0 && is_numeric($area) )
			$this->db->where("area = $area and delivered = 0 and deleted = 0  ");
		else
//			$this->db->where("delivered = 1 and deleted = 0 ");
			$this->db->where("delivered = 0 and deleted = 0 ");
		//	$this->db->join("customers","customers.id = order.customer_id");
		$this->db->order_by($this->pri_key,"DESC");
			
			$query= $this->db->get($this->table);
			
			
			if($query->num_rows() >0)
				
				{
			
		
					return $query->result_array();
				}
				return false;
	}
	public function list_data($area,$num,$off,$table=1)
	{
		
		$this->db->limit($off,$num);
		if($table = 1)
		{
			
		//	$this->db->join("customers","customers.id = order.customer_id");
			if($this->db->count_all($this->table) >0)
				//return $this->db->get($this->table)->result_array();
				{
			
		
					return $this->db->get($this->table)->result_array();
				}
				return false;
		}
		else
		{
			if($area > 0 && is_numeric($area)) 
				$this->db->where("order.area = $area");
			$this->db->join("products","products.id = order.prod_id");
			if($this->db->count_all($this->table_b) >0)
			//return $this->db->get($this->table)->result_array();
			{
		
	
				return $this->db->get($this->table_b)->result_array();
			}
			return false;			
		}
	}
	// dem so record 
	function count_all($table=1)
	{
		
		if($table == 4)
		{
			$this->db->where("deleted = 1 ");
			$query = $this->db->get($this->table);
			return $query->num_rows();
		}
	
		if($table == 2)
		{
			$this->db->where("delivered = 1 and deleted = 0 ");
			$query = $this->db->get($this->table);
			return $query->num_rows();
		}
		if($table == 3)
		{
			$this->db->where("delivered = 0 and deleted = 0 ");
			$query = $this->db->get($this->table);
			return $query->num_rows();
		}
		if($table == 1)
			return $this->db->count_all_results($this->table);
		else
			return $this->db->count_all_results($this->table_b);
	
	}
	
	
	
	





	function delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

			//	$row = $this->info_from_id($id);

		

				//xoa file

		//		if ($row['avatar'] != 'null') unlink($row['avatar']);

		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				$delete = array("deleted"=>"1");
				
				$this->update($delete,$id);
			
				//$this->db->where($this->pri_key, $id);
				//$this->db->delete($this->table); 
				//$this->db->query($sql);

			}			

		}

	}

function undelete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

			//	$row = $this->info_from_id($id);

		

				//xoa file

		//		if ($row['avatar'] != 'null') unlink($row['avatar']);

		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				$delete = array("deleted"=>"0");
				
				$this->update($delete,$id);
			
				//$this->db->where($this->pri_key, $id);
				//$this->db->delete($this->table); 
				//$this->db->query($sql);

			}			

		}

	}

}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>